<template>
  <div class="consultation">
    <h2>区域管理</h2>
    <el-button type="primary" size="mini" @click="addData">新增</el-button>
    <div class="consultation-block">
      <div class="table" v-loading="tableLoading">
        <el-table size="mini" :data="tableData" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="areaName" label="区域名称"> </el-table-column>
          <el-table-column prop="areaPosition" label="区域地址"> </el-table-column>
          <el-table-column prop="areaStatus" label="区域状态"> </el-table-column>
          <el-table-column prop="areaLevel" label="区域等级"> </el-table-column>
          <el-table-column prop="handler" label="受理员"> </el-table-column>
          <el-table-column prop="handlerPhone" label="受理员电话"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="warning"
                size="mini"
                @click="updateData(scope.row)"
                >修改</el-button
              >
              <el-button
                type="danger"
                size="mini"
                @click="deleteData(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page.sync="pageNum"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="isVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="form"
        label-width="120px"
        class="demo-form"
      >
        <el-form-item label="站点名称" prop="areaName">
          <el-input type="text" v-model="form.areaName"></el-input>
        </el-form-item>
        <el-form-item label="站点位置" prop="areaPosition">
          <el-input type="text" v-model="form.areaPosition"></el-input>
        </el-form-item>
        <el-form-item label="站点等级" prop="areaLevel">
          <el-select v-model="form.areaLevel" placeholder="请选择">
            <el-option
              v-for="item in optionsLevel"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="站点状态" prop="areaStatus">
          <el-select v-model="form.areaStatus" placeholder="请选择">
            <el-option
              v-for="item in optionsStatus"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="覆盖用户" prop="coverUsers">
          <el-select v-model="form.coverUsers" placeholder="请选择">
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.username"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="受理员名字" prop="handler">
          <el-input type="text" v-model="form.handler"></el-input>
        </el-form-item>
        <el-form-item label="受理员头像" prop="handlerAvator">
          <div v-if="form.handlerAvator" class="img-blocks">
            <div
              style="position: relative"
            >
              <img :src="form.handlerAvator" style="width: 80px" />
              <i @click="deleteImg()" class="el-icon-circle-close"></i>
            </div>
          </div>
          <el-upload 
            v-if="!form.handlerAvator"
            class="avatar-uploader"
            :action="'/api/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="受理员电话" prop="handlerPhone">
          <el-input type="text" v-model="form.handlerPhone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addArea, getArea, deleteArea, updateArea } from "../service.js";
export default {
  name: "home",
  mounted() {
    this.refreshTable();
  },
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      isVisible: false,
      dialogTitle: "",
      form: {
        areaName: "",
        areaPosition: "",
        coverUsers: [],
        handler: '',
        handlerAvator: '',
        handlerPhone: '',
        areaStatus: '',
        areaLevel: ''
      },
      rules: {
        areaName: [{ required: true, message: "请输入", trigger: "blur" }],
        areaPosition: [{ required: true, message: "请输入", trigger: "blur" }],
        handler: [{ required: true, message: "请输入", trigger: "blur" }],
        handlerPhone: [{ required: true, message: "请输入", trigger: "blur" }],
        handlerAvator: [{ required: true, message: "请上传", trigger: "blur" }],
        areaStatus: [{ required: true, message: "请选择", trigger: "blur" }],
        areaLevel: [{ required: true, message: "请选择", trigger: "blur" }],
        // coverUsers: [{ required: true, message: "请选择", trigger: "blur" }],
      },
      optionsLevel: ['低级', '中级', '高级'],
      optionsStatus: ['正常运作', '暂停运作'],
      userList: [],
      title: "",
      funList: [],
      checkList: ["选中且禁用", "复选框 A"],
    };
  },
  methods: {
    handleAvatarSuccess(result) {
      this.form.handlerAvator = "http://localhost:3000/" + result.imgName;
    },
    addUser() {
      this.isVisible = true;
      this.form = {
        username: "",
        pass: "",
        checkPass: "",
        title: "",
        column: "",
      };
      this.dialogTitle = "新建用户";
    },
    handleClose() {
      this.isVisible = false;
    },
    handleCurrentChange(index) {
      this.pageNum = index
      this.naviData()
    },
    submitForm(formName) {
      console.log(this.form)
      let form = this.form
      console.log(form);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.form.areaId) {
            addArea(form).then((res) => {
              console.log(res);
              if (res.code === 200) {
                this.$message.success("新增成功");
                this.isVisible = false;
                this.refreshTable();
              }
            });
            return;
          }
          updateArea(this.form.areaId, {
            areaName: form.areaName,
            pics: form.pics,
            types: form.types,
            funDesc: form.funDesc,
            price: form.price,
            priceVip: form.priceVip,
          }).then((res) => {
            if (res.code === 200) {
              this.$message.success("编辑成功");
              this.refreshTable();
              this.isVisible = false;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addData() {
      this.dialogTitle = "新增";
      this.isVisible = true;
    },
    refreshTable() {
      getArea(this.pageNum, this.pageSize).then((res) => {
        this.tableData = res.code === 200 ? res.data : [];
      });
    },
    detail(row) {
      this.json_fields = {};
      this.columnModel = JSON.parse(row.column);
      for (let i = 0; i < this.columnModel.length; i++) {
        this.json_fields[this.columnModel[i]] = i + "";
      }
      this.tableDataModal = JSON.parse(row.data);
      this.title = row.title;
      this.isVisible2 = true;
    },
    updateData(row) {
      this.form = {
        ...row,
      };
      this.dialogTitle = "修改";
      this.isVisible = true;
    },
    deleteData(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteArea(row.areaId).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.refreshTable();
            }
          });
        })
        .catch(() => {
          
        });
    },
    deleteImg() {
      this.form.handlerAvator = ''
    },
  },
};
</script>

<style scoped>
h2 {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.consultation-block {
  width: 90%;
  margin-left: 5%;
}

.pagination {
  text-align: center;
  margin-top: 20px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 60px;
  height: 60px;
  line-height: 60px;
  border: 1px dashed gray;
  text-align: center;
}
.avatar {
  width: 60px;
  height: 60px;
  display: block;
}
.img-blocks {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.el-icon-circle-close {
  position: absolute;
  top: -5px;
  right: -5px;
  cursor: pointer;
  color: red;
}
</style>
